package com.sxz.sbs.module.telephone;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.suke.widget.SwitchButton;
import com.sxz.sbs.R;
import com.sxz.sbs.app.GlideApp;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.event.OperateTelSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.telephone.bean.TelBookBean;
import com.sxz.sbs.module.telephone.presenter.TelBookDetailPresenter;
import com.sxz.sbs.module.telephone.view.TelBookDetailView;
import com.sxz.sbs.view.HorizontalCellView;
import com.sxz.sbs.widget.CommonTitle;
import com.sxz.sbs.widget.PhoneDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/8
 * 个人详情
 */

public class PersonageDetailsActivity extends BaseActivity<TelBookDetailPresenter> implements TelBookDetailView,
        SwitchButton.OnCheckedChangeListener{

    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.tel_user_name)
    TextView telUserName;
    @BindView(R.id.tel_user_head)
    ImageView telUserHead;
    @BindView(R.id.iv_tel_user_car)
    ImageView ivStar;
    @BindView(R.id.tel_user_phone)
    TextView telUserPhone;
    @BindView(R.id.hcv_tel_user_name)
    HorizontalCellView hcvTelUserName;
    @BindView(R.id.hcv_tel_user_company)
    HorizontalCellView hcvTelUserCompany;
    @BindView(R.id.hcv_tel_user_department)
    HorizontalCellView hcvTelUserDepartment;
    @BindView(R.id.hcv_tel_user_job)
    HorizontalCellView hcvTelUserJob;
    private TelBookBean.ManagerListBean intentBean;
    private boolean isAdd;
    private int initMark = 0;
    private int initTag;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("详情").build().show();
        intentBean = getIntent().getParcelableExtra(IntentConstant.TEL_BOOK_DETAIL);
        if (intentBean != null) {
            initMark = intentBean.getMark();
            initTag = intentBean.getMark();
            telUserName.setText(intentBean.getTxt_Name());
            telUserPhone.setText(intentBean.getTxt_Phone());
            GlideApp.with(this).load(intentBean.getUrl_HeadPhoto()).circleCrop()
                    .error(R.drawable.ic_headportrait).placeholder(R.drawable.ic_headportrait)
                    .into(telUserHead);
            hcvTelUserName.setRightText(intentBean.getTxt_Name());
            hcvTelUserCompany.setRightText(intentBean.getCompanyName());
            hcvTelUserDepartment.setRightText(intentBean.getTxt_DepartmentName());
            hcvTelUserJob.setRightText(intentBean.getTxt_PositionName());
            if (intentBean.getMark() != 1) {
                ivStar.setVisibility(View.GONE);
                switchButton.setOnCheckedChangeListener(null);
                switchButton.setChecked(false);
            } else {
                ivStar.setVisibility(View.VISIBLE);
                switchButton.setOnCheckedChangeListener(null);
                switchButton.setChecked(true);
            }

        }

       switchButton.setOnCheckedChangeListener(this);

    }
    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (intentBean != null) {
            HashMap<String, Object> map = HttpParamMap.getParamMap();
            map.put("managerId", intentBean.getId());
            if (isChecked) {
                isAdd = true;
                mPresenter.addMangerMark(map);
            } else {
                isAdd = false;
                mPresenter.cancelMangerMark(map);
            }
        }
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_personagedetails;
    }

    @Override
    protected TelBookDetailPresenter createPresenter() {
        return new TelBookDetailPresenter();
    }

    @Override
    public void showError(int errorTag, String msg) {
        super.showError(errorTag, msg);
        if (isAdd) {
            switchButton.setOnCheckedChangeListener(null);
            switchButton.setChecked(false);
        } else {
            switchButton.setOnCheckedChangeListener(null);
            switchButton.setChecked(true);
        }
    }

    @Override
    public void onAddMangerMarkSuccess() {
        ivStar.setVisibility(View.VISIBLE);
        initTag = 1;
    }

    @Override
    public void onCancelMangerMarkSuccess() {
        ivStar.setVisibility(View.GONE);
        initTag = 0;
    }


    @OnClick(R.id.tel_user_phone)
    public void onViewClicked() {
        if (intentBean != null) {
            PhoneDialog.callPhone(this,intentBean.getTxt_Phone());
        }

    }

    @Override
    protected void onDestroy() {
        if (initMark != initTag) {
            EventBus.getDefault().post(new OperateTelSuccessEvent());
        }
        super.onDestroy();
    }
}
